package sfsoftware.game.actor.enemy.bubblefish;

import sfsoftware.game.trevor.Player;
import sfsoftware.util.Tile;

public class FuryFish extends SimpleBubbleFish {
	double directionAngle;
	
	public FuryFish(int destX, int destY, String texture, int destWidth, int destHeight, Tile[][] assocTileMap) {
		super(destX, destY, texture, destWidth, destHeight, assocTileMap);		
		
		setSrcX(120);
		setSpeed(1);
		
		maxHealth = 3;
		health = 3;
	}
	
	private double getAngleToPlayer()
	{
		double dx = (getX() + (getWidth() / 2)) - (Player.getXPos() + 20);
		double dy = (getY() + (getHeight() / 2)) - (Player.getYPos() + 20);
		double inRads = Math.atan2(dy,dx);
		
		if (inRads < 0)
		{
	        inRads = Math.abs(inRads);
		}
	    else
	    {
	        inRads = 2 * Math.PI - inRads;
	    }			
		
		return inRads;
	}
	
	public void update()
	{
		if(!(isInvulnerable()))
		{
			// If we are 164 pixels or closer, double speed!
			if(distanceToPlayer() <= 164 && !(Player.getInvulnerable()))
			{
				setSpeed(2);
			}
			else
			{
				setSpeed(1);
			}
					
			// Update the angle from the fury fish to the player
			directionAngle = getAngleToPlayer();		
			double xDirection = Math.cos(directionAngle) * -1;
			double yDirection = Math.sin(directionAngle);
			
			// Try to move!
			move(xDirection, yDirection, tileMap);
		}
		
		// Update the animation frame
		if(animationTimer.updateClock())
		{
			if(getSrcY() == 0)
			{
				setSrcY(40);
			}
			else
			{
				setSrcY(0);
			}
		}
	}

}
